package com.ltmonitor.web.controller.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.LineSegment;
import com.ltmonitor.entity.MapArea;
import com.ltmonitor.entity.RouteSegment;
import com.ltmonitor.service.ILineSegmentService;
import com.ltmonitor.service.IMapAreaBindingService;
import com.ltmonitor.service.IMapAreaService;
import com.ltmonitor.service.IRouteSegmentService;
import com.ltmonitor.service.ITerminalService;
import com.ltmonitor.service.MapFixService;
import com.ltmonitor.util.Constants;
import com.ltmonitor.util.StringUtil;
import com.ltmonitor.vo.PointLatLng;
import com.ltmonitor.web.constants.FunctionConstant;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;

/**
 * 区域设置
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/mapArea")
public class MapAreaAction extends GenericAction {

	private static final String SEGMENTS_KEY = "LINE_SEGMENTS";

	@Autowired
	private IMapAreaService mapAreaService;
	@Autowired
	private IMapAreaBindingService mapAreaBindingService;

	@Autowired
	private IRouteSegmentService routeSegmentService;

	@Autowired
	private ILineSegmentService lineSegmentService;

	/**
	 * 查看线路信息
	 * 
	 * @return
	 */
	@RequestMapping("/viewArea")
	public String viewArea(@RequestParam(defaultValue = "0") long entityId,
			String areaType, @RequestParam(defaultValue = "0") double radius,
			String strRoutePoints, Model m) {
		MapArea mapArea = new MapArea();
		if (entityId > 0) {
			mapArea = mapAreaService.load(entityId);
		}
		if (StringUtil.isNullOrEmpty(areaType) == false) {
			mapArea.setAreaType(areaType);
		} else
			areaType = (mapArea.getAreaType());

		if (mapArea.getEntityId() == 0) {
			mapArea.setRadius((int) radius);
			mapArea.setPoints(strRoutePoints);
		}
		String allowEdit = this.isAllowEdit(mapArea) ? "yes" : "no";
		getRequest().setAttribute("allowEdit", allowEdit);

		m.addAttribute("entity", mapArea);
		m.addAttribute("allowEdit", allowEdit);
		return "/mapArea/editArea";

	}

	/**
	 * 判断用户是否有编辑区域和线路的权限
	 * @param mapArea
	 * @return
	 */
	private boolean isAllowEdit(MapArea mapArea) {
		String permissionName = "";
		String areaType = mapArea.getAreaType();
		if (MapArea.CIRCLE.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_CIRCLE;
		} else if (MapArea.RECT.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_RECT;
		} else if (MapArea.POLYGON.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_POLOGON;
		}else if (MapArea.MARKER.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_MARKER;
		}else if (MapArea.KEY_POINT.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_KEYPOINT;
		}else if (MapArea.ROUTE.equals(areaType)) {
			permissionName = FunctionConstant.FUNC_ROUTE;
		}

		OnlineUser u = getOnlineUser();
		if (mapArea.getEntityId() == 0) {
			return isAuthorized(permissionName);
		} else {
			if (u.isSuperAdmin()) {
				return true;
			}
			permissionName = FunctionConstant.FUNC_EDIT_MAP_AREA;
			// 是否有编辑权限
			if (isAuthorized(permissionName)) {
				List<Long> depIdList = super.getAuthorizedDepIdList();
				if (depIdList.contains(mapArea.getCompanyId())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 查看关键点信息
	 * 
	 * @return
	 */
	@RequestMapping("/viewKeyPoint")
	public String viewKeyPoint(@RequestParam(defaultValue = "0") long entityId,
			String areaType, String strRoutePoints, Model m) {
		MapArea mapArea = new MapArea();
		if (entityId > 0) {
			mapArea = mapAreaService.load(entityId);
		}
		if (StringUtil.isNullOrEmpty(areaType) == false) {
			String ecType = "";
			if ("circle".equals(areaType))
				ecType = MapArea.CIRCLE;
			else if ("rect".equals(areaType))
				ecType = MapArea.RECT;
			else if ("polygon".endsWith(areaType))
				ecType = MapArea.POLYGON;
			mapArea.setAreaType(ecType);
		} else
			areaType = (mapArea.getAreaType());
		if (mapArea.getEntityId() == 0) {
			// mapArea.setRadius(radius);
			mapArea.setPoints(strRoutePoints);
		}
		String allowEdit = this.isAllowEdit(mapArea) ? "yes" : "no";
		m.addAttribute("allowEdit", allowEdit);
		m.addAttribute("entity", mapArea);
		return "/mapArea/editKeyPoint";

	}

	// 查看分段限速点信息
	@RequestMapping("/viewRouteSegment")
	public String viewRouteSegment(
			@RequestParam(defaultValue = "0") long entityId, Model m) {
		try {
			RouteSegment routeSegment = routeSegmentService.load(entityId);
			m.addAttribute("routeSegment", routeSegment);
		} catch (Exception ex) {
			m.addAttribute("message", ex.getMessage());
		}
		return "/mapArea/editLimitSpeed";
	}

	// 保存分段限速点信息
	@RequestMapping("/saveRouteSegment.action")
	@ResponseBody
	public JsonMessage saveRouteSegment(RouteSegment routeSegment, Model m) {
		try {
			this.routeSegmentService.saveOrUpdate(routeSegment);
			return json(true, routeSegment);
		} catch (Exception ex) {
			m.addAttribute("message", ex.getMessage());
			return json(false, ex.getMessage());
		}
		// return "success";
	}

	@RequestMapping("/deleteLimitSpeedPoint.action")
	@ResponseBody
	public JsonMessage deleteLimitSpeedPoint(
			@RequestParam(defaultValue = "0") long entityId) {

		try {
			Map params = super.getParams();
			this.routeSegmentService.deleteById(entityId);
			return super.json(true, "记录已被成功刪除!");
		} catch (Exception e) {
			return json(false, e.getMessage());
		}
	}

	// 查看分段限速点信息
	@RequestMapping("/saveLimitSpeedPoint.action")
	@ResponseBody
	public JsonMessage saveLimitSpeedPoint(LineSegment lineSegment, long routeId) {
		try {
			List needSaveList = new ArrayList();
			// 对于线路中新插入的分段限速点，需要对线路上所有的点进行重新排序
			if (lineSegment.getEntityId() == 0) {
				String hql = "from LineSegment where routeId = ?  order by pointId";
				List segments = this.lineSegmentService.query(hql,
						new Object[] { lineSegment.getRouteId() });

				PointLatLng p0 = new PointLatLng(lineSegment.getLongitude1(),
						lineSegment.getLatitude1());

				boolean isOnRoute = false;
				int pointId = 0;
				int startPointId = 0;
				int endPointId = 0;
				if (segments.size() > 0) {
					LineSegment s = (LineSegment) segments
							.get(segments.size() - 1);
					endPointId = s.getPointId() + 1;
				}

				String hsql = "delete from RouteSegment where routeId = ?";
				this.routeSegmentService.bulkUpdate(hsql,
						new Object[] { routeId });
				int segNo = 1;
				LineSegment prevSegment = null;
				LineSegment prevLimitSpeedPoint = null;
				for (Object obj : segments) {
					LineSegment seg = (LineSegment) obj;

					if (isOnRoute == false) {
						if (prevSegment != null) {
							PointLatLng p1 = new PointLatLng(
									prevSegment.getLongitude1(),
									prevSegment.getLatitude1());
							PointLatLng p2 = new PointLatLng(
									seg.getLongitude1(), seg.getLatitude1());
							isOnRoute = MapFixService.isPointOnRouteSegment(p1,
									p2, p0, 10);

							if (isOnRoute) {
								pointId = prevSegment.getPointId() + 1;
								lineSegment.setPointId(pointId++);
								lineSegment.setName("分段点:"
										+ lineSegment.getPointId());

								seg.setPointId(pointId++);
								needSaveList.add(seg);

								RouteSegment rs2 = new RouteSegment();
								rs2.setRouteId(routeId);
								rs2.setStartSegId(startPointId);
								rs2.setName("分段" + segNo++);
								rs2.setEndSegId(lineSegment.getPointId());
								needSaveList.add(rs2);
								startPointId = lineSegment.getPointId();
							}
						}
					} else {
						seg.setPointId(pointId++);
						needSaveList.add(seg);
					}

					if (seg.isStation()) {
						// 上一个分段点
						RouteSegment rs1 = new RouteSegment();
						rs1.setRouteId(routeId);
						rs1.setStartSegId(startPointId);
						rs1.setName("分段" + segNo++);
						rs1.setEndSegId(seg.getPointId());
						needSaveList.add(rs1);
						startPointId = seg.getPointId();
					}

					prevSegment = seg;

				}

				if (isOnRoute == false || lineSegment.getPointId() == 0) {
					return json(false, "限速点没有在线段上");
				}

				RouteSegment rs1 = new RouteSegment();
				rs1.setRouteId(routeId);
				rs1.setStartSegId(startPointId);
				rs1.setName("分段" + segNo++);
				rs1.setEndSegId(endPointId);
				needSaveList.add(rs1);

			}

			lineSegment.setStation(true);
			needSaveList.add(lineSegment);
			this.lineSegmentService.saveOrUpdateAll(needSaveList);

			return json(true, lineSegment);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 查看marker页面
	 * 
	 * @return
	 */

	@RequestMapping("/viewMarker.action")
	public String viewMarker(@RequestParam(defaultValue = "0") long entityId,
			String areaType, String strRoutePoints, Model m) {
		MapArea mapArea = new MapArea();
		if (entityId > 0) {
			mapArea = mapAreaService.load(entityId);
		}
		if (StringUtil.isNullOrEmpty(areaType) == false) {
			mapArea.setAreaType(areaType);
		} else
			areaType = (mapArea.getAreaType());
		if (mapArea.getEntityId() == 0) {
			mapArea.setPoints(strRoutePoints);
		}


		String allowEdit = this.isAllowEdit(mapArea) ? "yes" : "no";
		m.addAttribute("entity", mapArea);
		getRequest().setAttribute("allowEdit", allowEdit);
		return "/mapArea/editMarker";

	}

	@RequestMapping("/saveArea.action")
	@ResponseBody
	public JsonMessage saveArea(MapArea mapArea, String[] alarmTypes,
			@RequestParam(defaultValue = "0") double radius,
			String originGpsCoord, Model m) {
		try {
			if (alarmTypes == null || alarmTypes.length == 0) {
				return json(false, "报警类型至少要选择一种");
			}

			// mapArea.setKeyPoint(keyPoint);
			// enclosure.setEnclosureType(Enclo); // 设置围栏类型为线路
			mapArea.setRadius(radius);

			if (mapArea.getEntityId() == 0) {
				OnlineUser u = getOnlineUser();
				mapArea.setOwner("" + u.getEntityId());
				mapArea.setMapType(getMapType());

				if (Constants.MAP_GPS.equals(originGpsCoord)) {
					mapArea.setMapType(Constants.MAP_GPS);
				}
			}

			PointLatLng pt = getPolygonCenter(mapArea.getPoints());
			mapArea.setCenterLat(pt.getLat());
			mapArea.setCenterLng(pt.getLng());

			StringBuilder sb = new StringBuilder();
			for (String alarm : alarmTypes) {
				sb.append(alarm).append(",");
			}
			mapArea.setByTime(isChecked("byTime"));
			mapArea.setLimitSpeed(isChecked("limitSpeed"));

			mapArea.setAlarmType(sb.toString());
			mapArea.setCompanyId(this.getOnlineUser().getCompanyId());
			mapAreaService.saveOrUpdate(mapArea);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, "保存失败:" + ex.getMessage());
		}
		return json(true, mapArea);
	}

	@RequestMapping("/saveKeyPoint.action")
	@ResponseBody
	public JsonMessage saveKeyPoint(MapArea mapArea, String[] alarmTypes,
			@RequestParam(defaultValue = "0") int keyPoint, double radius,
			String originGpsCoord, Model m) {
		try {

			mapArea.setKeyPoint(keyPoint);
			mapArea.setRadius(radius);

			PointLatLng pt = getPolygonCenter(mapArea.getPoints());
			mapArea.setCenterLat(pt.getLat());
			mapArea.setCenterLng(pt.getLng());

			mapArea.setByTime(isChecked("byTime"));
			mapArea.setLimitSpeed(isChecked("limitSpeed"));

			mapArea.setAlarmType("关键点报警");
			mapArea.setCompanyId(this.getOnlineUser().getCompanyId());

			mapAreaService.saveOrUpdate(mapArea);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, "保存失败:" + ex.getMessage());
		}
		return json(true, mapArea);
	}

	/**
	 * 保存地标信息
	 * 
	 * @return
	 */
	@RequestMapping("/saveMarker.action")
	@ResponseBody
	public JsonMessage saveMarker(MapArea mapArea, Model m) {
		try {

			// mapArea.setKeyPoint(keyPoint);
			// enclosure.setEnclosureType(Enclo); // 设置围栏类型为线路
			// mapArea.setRadius(radius);
			mapArea.setMapType(getMapType());
			if (mapArea.getEntityId() == 0) {
				OnlineUser u = getOnlineUser();
				mapArea.setOwner("" + u.getEntityId());
			}

			PointLatLng pt = getPolygonCenter(mapArea.getPoints());
			mapArea.setCenterLat(pt.getLat());
			mapArea.setCenterLng(pt.getLng());
			mapArea.setCompanyId(this.getOnlineUser().getCompanyId());
			mapAreaService.saveOrUpdate(mapArea);

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, "保存失败:" + ex.getMessage());
		}
		return json(true, mapArea);
	}

	/**
	 * 查看线路信息
	 * 
	 * @return
	 */
	@RequestMapping("/viewRoute")
	public String viewRoute(@RequestParam(defaultValue = "0") long entityId,
			String areaType, String strRoutePoints, Model m) {

		try {
			MapArea mapArea = new MapArea();
			mapArea.setAreaType(MapArea.ROUTE);
			if (entityId > 0) {
				mapArea = mapAreaService.load(entityId);
			}
			List<LineSegment> lineSegments = new ArrayList<LineSegment>();
			if (mapArea.getEntityId() > 0) {
				lineSegments = this.lineSegmentService.getLineSegments(mapArea
						.getEntityId());

				List<RouteSegment> routeSegments = this.routeSegmentService
						.getRouteSegments(mapArea.getEntityId());

				m.addAttribute("routeSegments", routeSegments);
				// this.putSession("routeSegments", routeSegments);
			} else if (strRoutePoints != null) {

				PointLatLng ptCenter = getPolygonCenter(strRoutePoints);
				mapArea.setCenterLat(ptCenter.getLat());
				mapArea.setCenterLng(ptCenter.getLng());

				String[] points = strRoutePoints.split(";");
				int k = 0;
				for (String strPt : points) {
					if (StringUtil.isNullOrEmpty(strPt))
						continue;
					String[] pt = strPt.split(",");
					Double lng = Double.parseDouble(pt[0]);
					Double lat = Double.parseDouble(pt[1]);

					LineSegment ls = new LineSegment();
					ls.setPointId(k);
					ls.setLatitude1(lat);
					ls.setLongitude1(lng);
					ls.setName("拐点" + k);
					// ls.setLineWidth(50);
					lineSegments.add(ls);
					k++;
				}
				int totalCount = lineSegments.size();
				for (k = 0; k < totalCount; k++) {
					LineSegment seg1 = lineSegments.get(k);

					if (k < (totalCount - 1)) {
						LineSegment seg2 = lineSegments.get(k + 1);
						seg1.setLatitude2(seg2.getLatitude1());
						seg1.setLongitude2(seg2.getLongitude1());
					}
				}
				mapArea.setPoints(strRoutePoints);
			}

			m.addAttribute("lineSegments", lineSegments);
			m.addAttribute("entity", mapArea);
			this.putSession(SEGMENTS_KEY, lineSegments);

			String allowEdit = this.isAllowEdit(mapArea) ? "yes" : "no";
			getRequest().setAttribute("allowEdit", allowEdit);
		} catch (Exception ex) {
			m.addAttribute("message", ex.getMessage());
		}
		return "/mapArea/editRoute";
	}

	// 如果参数被选中，在rquest参数中，就包含了参数值
	protected boolean isChecked(String paramName) {
		return this.getParams().containsKey(paramName);
	}

	/**
	 * 保存线路
	 * 
	 * @return
	 */

	@RequestMapping("/saveRoute.action")
	@ResponseBody
	public JsonMessage saveRoute(MapArea mapArea, String[] alarmTypes,
			String originGpsCoord, Model m) {
		try {
			if (alarmTypes == null || alarmTypes.length == 0) {
				return json(false, "报警类型至少要选择一种");
			}

			mapArea.setAreaType(MapArea.ROUTE); // 设置围栏类型为线路

			StringBuilder sb = new StringBuilder();
			for (String alarm : alarmTypes) {
				sb.append(alarm).append(",");
			}
			mapArea.setByTime(isChecked("byTime"));
			mapArea.setLimitSpeed(isChecked("limitSpeed"));
			mapArea.setMapType(getMapType());

			if (mapArea.getEntityId() == 0) {
				OnlineUser u = getOnlineUser();
				mapArea.setOwner("" + u.getEntityId());

				if (Constants.MAP_GPS.equals(originGpsCoord)) {
					mapArea.setMapType(Constants.MAP_GPS);
				}
			}
			PointLatLng ptCenter = getPolygonCenter(mapArea.getPoints());
			mapArea.setCenterLat(ptCenter.getLat());
			mapArea.setCenterLng(ptCenter.getLng());
			mapArea.setAlarmType(sb.toString());
			mapArea.setCompanyId(this.getOnlineUser().getCompanyId());
			List<LineSegment> lineSegments = (List<LineSegment>) this
					.getFromSession(SEGMENTS_KEY);
			mapAreaService.saveRoute(mapArea, lineSegments);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, "保存失败:" + ex.getMessage());
		}
		return json(true, mapArea);
	}

	/**
	 * 保存线路中的某一个拐点信息
	 * 
	 * @return
	 */
	@RequestMapping("/saveLineSegment.action")
	public String saveLineSegment(int pointId, Model m) {
		LineSegment lineSegment = getSegment(pointId);

		if (lineSegment != null) {
			try {
				BeanUtils.copyProperties(lineSegment, super.getParams());

				lineSegment.setByTime(isChecked("byTime"));
				lineSegment.setLimitSpeed(isChecked("limitSpeed"));
				lineSegment.setStation(isChecked("station"));

				if (lineSegment.getEntityId() > 0) {
					this.lineSegmentService.saveOrUpdate(lineSegment);
				}

				lineSegment.setCompanyId(this.getOnlineUser().getCompanyId());
			} catch (Exception e) {
				log.error(e.getMessage(), e);
				m.addAttribute("message", "保存失败" + e.getMessage());
				// throw new ClassInstantiateException(e);

			}
		}
		m.addAttribute("lineSegment", lineSegment);
		m.addAttribute("message", "保存成功");
		return "/mapArea/editLineSegment";
	}

	@RequestMapping("/viewLineSegment.action")
	public String viewLineSegment(int pointId, Model m) {
		LineSegment lineSegment = getSegment(pointId);

		if (lineSegment != null) {
			try {
				// BeanUtils.copyProperties(lineSegment, super.getParams());

				// lineSegment.setByTime(isChecked("byTime"));
				// lineSegment.setLimitSpeed(isChecked("limitSpeed"));
				// lineSegment.setStation(isChecked("station"));

			} catch (Exception e) {
				log.error(e.getMessage(), e);
				m.addAttribute("message", "保存失败" + e.getMessage());
				// throw new ClassInstantiateException(e);

			}
		} else
			lineSegment = new LineSegment();

		m.addAttribute("lineSegment", lineSegment);
		// m.addAttribute("message", "保存成功");
		return "/mapArea/editLineSegment";

	}

	@RequestMapping("/deleteAreas.action")
	@ResponseBody
	public JsonMessage deleteAreas(String areaIds) {
		try {
			List<MapArea> result = new ArrayList<MapArea>();
			if (StringUtil.isNullOrEmpty(areaIds) == false) {
				String[] idarray = areaIds.split(",");
				for (String strEnclosureId : idarray) {
					long areaId = Long.parseLong(strEnclosureId);
					MapArea ec = (MapArea) this.mapAreaService.load(areaId);
					ec.setDeleted(true);
					// 删除区域绑定关系
					String hql = "delete from MapAreaBinding where areaId = ?";
					this.mapAreaBindingService.bulkUpdate(hql,
							new Object[] { ec.getEntityId() });
					result.add(ec);

					if (MapArea.ROUTE.equals(ec.getAreaType())) {
						String hsql = "delete from LineSegment where routeId = ?";
						this.lineSegmentService.bulkUpdate(hsql,
								new Object[] { ec.getEntityId() });

						hsql = "delete from RouteSegment where routeId = ?";
						this.routeSegmentService.bulkUpdate(hsql,
								new Object[] { ec.getEntityId() });
					}
				}

			}
			if (result.size() > 0) {
				this.mapAreaService.saveOrUpdateAll(result);
			}
			return json(true, "");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 
	 * @return
	 */
	public JsonMessage testOffsetRoute(double latitude, double longitude) {
		List<LineSegment> lineSegments = this.lineSegmentService
				.getLineSegments(117);

		for (LineSegment ls : lineSegments) {
			PointLatLng p1 = new PointLatLng(ls.getLongitude1(),
					ls.getLatitude1());
			PointLatLng p2 = new PointLatLng(ls.getLongitude2(),
					ls.getLatitude2());
			PointLatLng p0 = new PointLatLng(longitude, latitude);
			boolean result = MapFixService
					.isPointOnRouteSegment(p1, p2, p0, 25);
			return json(result, result ? "在线端上" : "偏离");
		}
		return json(false, "无线路数据");
	}

	public String queryVehicleInArea() {

		return "success";
	}

	@ResponseBody
	@RequestMapping("/getMapAreaList.action")
	public JsonMessage getMapAreaList() {
		try {
			// 查询出地图电子围栏、地图标注、线路路
			Collection<MapArea> areas = getAuthoriedMapAreaList();

			String hql = "from LineSegment where station = ?";
			// 分段限速点
			List<LineSegment> limitSpeedPoints = this.lineSegmentService.query(
					hql, true);
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("areas", areas);
			result.put("limitSpeedPoints", limitSpeedPoints);
			return json(true, result);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	private Collection<MapArea> getAuthoriedMapAreaList() {
		if (this.getOnlineUser().isSuperAdmin()) {
			// 超级用户能看到所有区域
			return this.mapAreaService.loadAll();
		} else {
			// 根据用户所属的部门，查询区域
			List<Long> parentIdList = this.departmentService
					.getParentDepIdList(this.getOnlineUser().getCompanyId());
			parentIdList.add(0L);
			List<MapArea> ls = this.mapAreaService.getMapAreaList(parentIdList);
			return ls;
		}
	}

	private List<PointLatLng> getPoints(String strPoints) throws Exception {
		List<PointLatLng> result = new ArrayList<PointLatLng>();

		try {
			String[] points = strPoints.split(";");
			for (String strPt : points) {
				if (StringUtil.isNullOrEmpty(strPt))
					continue;
				String[] pt = strPt.split(",");
				Double lng = Double.parseDouble(pt[0]);
				Double lat = Double.parseDouble(pt[1]);
				result.add(new PointLatLng(lng, lat));
			}
			return result;
		} catch (Exception ex) {
			throw new Exception("非法的坐标点格式");
		}
	}

	// 得到多边形的中心
	private PointLatLng getPolygonCenter(String strPoints) throws Exception {
		List<PointLatLng> ls = getPoints(strPoints);
		if (ls.size() == 1)
			return ls.get(0);
		double maxLat = 0, maxLng = 0, minLat = 200, minLng = 200;

		for (PointLatLng pl : ls) {
			if (pl.getLat() > maxLat)
				maxLat = pl.getLat();

			if (pl.getLng() > maxLng)
				maxLng = pl.getLng();

			if (pl.getLat() < minLat)
				minLat = pl.getLat();

			if (pl.getLng() < minLng)
				minLng = pl.getLng();
		}

		double lat1 = minLat + (maxLat - minLat) / 2;
		double lng1 = minLng + (maxLng - minLng) / 2;

		return new PointLatLng(lng1, lat1);
	}

	private LineSegment getSegment(int segId) {
		List<LineSegment> result = (List<LineSegment>) this
				.getFromSession(SEGMENTS_KEY);
		for (LineSegment ls : result) {
			if (ls.getPointId() == segId)
				return ls;
		}
		return null;
	}

}
